package com.dhcc.test.sevlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import com.dhcc.test.entityBinding.User;
/**
 * @description ：TODO
 * @date ：2016年11月25日 下午4:06:54
 * @author ：WangYG
 */
public class MyFristSevlet extends HttpServlet{
    
    private static final long serialVersionUID = 1L;

    @Override
    public void init() throws ServletException {
        System.out.println("MyFristSevlet 我被初始化了。。。。。");
        super.init();
    }
    
    @Override
    protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
        System.out.println("MyFristSevlet  执行我的service 方法了。。。。。。");
        //arg1.getWriter().write("into service function Success !!! ");
        super.service(arg0, arg1);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("MyFristSevlet  执行我的doGet()方法。。。。。。");
        //ServletResponse sresp = resp;
        //sresp.setCharacterEncoding("gbk");
        //sresp.setContentType("text/html;charset=gbk");
        PrintWriter out = resp.getWriter();
        String str = "into doGet function Success !!! 执行我的doGet()方法。。。。";
        out.write(str);
        usedRequestBuiltInmethod(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("MyFristSevlet  执行我的doPost()方法。。。。。。");
        //resp.getWriter().write("into doPost function Success !!! ");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println("username="+username+";password="+password);
        if(StringUtils.isNotBlank(username) && StringUtils.isNotBlank(password)){
            User user = (User) req.getSession().getAttribute("USER_IN_SESSION");
            if(null == user){
                user = new User();
                user.setName(username);
                user.setPassword(password);
                req.getSession().setAttribute("USER_IN_SESSION",user);
                req.getSession().removeAttribute("AccessError");//登录成功后移除错误信息
            }
            req.getRequestDispatcher("/index.jsp").forward(req, resp);
        }else{
            req.setAttribute("errorMsg", "请输入用户名和密码，进行登录！");
        }
    }

    @Override
    public void destroy() {
        System.out.println("MyFristSevlet 被销毁了！ 执行我的destroy()方法。。。。。。");
        super.destroy();
    }
    
    public void usedRequestBuiltInmethod(HttpServletRequest req, HttpServletResponse resp) throws IOException{
        PrintWriter out = resp.getWriter();
        System.out.println("获取服务器(本机)Ip地址："+req.getLocalAddr());
        System.out.println("获取服务器(本机)端口号："+req.getLocalPort());
        System.out.println("获取服务器(本机)用户名："+req.getLocalName());
        System.out.println("获取Web服务器名字："+req.getServerName());
        System.out.println("服务器监听的端口："+req.getServerPort());
        System.out.println("获取服务器版本信息："+req.getServletContext().getServerInfo());
        System.out.println("获取客户端Ip地址："+req.getRemoteAddr());
        System.out.println("获取客户端端口号："+req.getRemotePort());
        System.out.println("获取客户端主机名："+req.getRemoteHost());
        System.out.println("获取客户端用户："+req.getRemoteUser());
        System.out.println("获取请求方式："+req.getMethod());
        System.out.println("获取字符编码格式："+req.getCharacterEncoding());
        System.out.println("获取请求的数据类型："+req.getContentType());
        System.out.println("获取请求的URI："+req.getRequestURI());
        System.out.println("获取请求的URL："+req.getRequestURL());
        System.out.println("URL中Servlet路径之后、查询字符串之前的那部分。URL中的附加路径信息："+req.getPathInfo());
        System.out.println("URL中调用Servlet的那一部分，不包含附加路径信息和查询字符串："+req.getServletPath());
        System.out.println("web应用目录："+req.getContextPath());
        System.out.println("获取请求的查询字符串："+req.getQueryString());
        System.out.println("获取客户端请求的Session对象："+req.getSession());
        System.out.println("获取客户端请求的Session-ID："+req.getRequestedSessionId());
        
        
        System.out.println("获取客户端请求头信息："+req.getHeader("Accept"));
        System.out.println("获取客户端请求头信息中的语言设置编码格式："+req.getHeader("Accept-Language"));
        System.out.println("获取客户端请求头信息中的客户端浏览器信息："+req.getHeader("User-Agent"));
        

        
        out.write("<br>");
        out.write("获取服务器(本机)Ip地址："+req.getLocalAddr()+"<br>");
        out.write("获取服务器(本机)端口号："+req.getLocalPort()+"<br>");
        out.write("获取服务器(本机)用户名："+req.getLocalName()+"<br>");
        out.write("获取Web服务器名字："+req.getServerName()+"<br>");
        out.write("服务器监听的端口："+req.getServerPort()+"<br>");
        out.write("获取服务器版本信息："+req.getServletContext().getServerInfo()+"<br>");
        out.write("获取客户端Ip地址："+req.getRemoteAddr()+"<br>");
        out.write("获取客户端端口号："+req.getRemotePort()+"<br>");
        out.write("获取客户端主机名："+req.getRemoteHost()+"<br>");
        out.write("获取客户端用户："+req.getRemoteUser()+"<br>");
        out.write("获取请求方式："+req.getMethod()+"<br>");
        out.write("获取字符编码格式："+req.getCharacterEncoding()+"<br>");
        out.write("获取请求的数据类型："+req.getContentType()+"<br>");
        out.write("获取请求的URI："+req.getRequestURI()+"<br>");
        out.write("获取请求的URL："+req.getRequestURL()+"<br>");
        out.write("URL中Servlet路径之后、查询字符串之前的那部分。URL中的附加路径信息："+req.getPathInfo()+"<br>");
        out.write("URL中调用Servlet的那一部分，不包含附加路径信息和查询字符串："+req.getServletPath()+"<br>");
        out.println("web应用目录："+req.getContextPath()+"<br>");
        out.write("获取请求的查询字符串："+req.getQueryString()+"<br>");
        out.write("获取客户端请求的Session对象："+req.getSession()+"<br>");
        out.write("获取客户端请求的Session-ID："+req.getRequestedSessionId()+"<br>");
        
        
        out.write("获取客户端请求头信息："+req.getHeader("Accept")+"<br>");
        out.write("获取客户端请求头信息中的语言设置编码格式："+req.getHeader("Accept-Language")+"<br>");
        out.write("获取客户端请求头信息中的客户端浏览器信息："+req.getHeader("User-Agent")+"<br>");
        out.close();
        
    }
    
    public void print(String json,HttpServletResponse response) throws Exception{
        /*req.setCharacterEncoding("utf-8");
        //设置设置服务器端的编码,response缓冲区的编码  
         resp.setCharacterEncoding("UTF-8");  
         //设置通知浏览器服务器发送的数据格式,浏览器打开文件的编码  
         resp.setHeader("Content-Type", "text/html;charset=UTF-8");  
         //上面两句简写方式  
         resp.setContentType("text/html;charset=UTF-8");*/
        
        response.setHeader("Cache-Control",
                "no-store,no-cache,must-revalidate");
        response.setHeader("Pragma", "no-cache");
        response.addHeader("Content-Encoding", "gzip");
        response.setContentType("text/html");
        response.setCharacterEncoding("utf-8");
        response.getWriter().print(json);
        response.getWriter().close();
    }
    
}
